import java.util.Scanner;

/**
 * @author gao
 * @date 2025/7/1 22:41
 */

class ListNode {
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur1 = l1, cur2 = l2;
        // 虚拟头节点
        ListNode newHead = new ListNode(0);
        // 方便尾插
        ListNode prev = newHead;
        int t = 0;

        // 防止第一位是9，当t=1时，还要向前进一位
        while (cur1 != null || cur2 != null || t != 0) {
            // 如果cur1不为空，则将cur1的值加到t上，并将cur1指向下一个节点
            if (cur1 != null) {
                t += cur1.val;
                cur1 = cur1.next;
            }
            // 如果cur2不为空，则将cur2的值加到t上，并将cur2指向下一个节点
            if (cur2 != null) {
                t += cur2.val;
                cur2 = cur2.next;
            }
            prev.next = new ListNode(t % 10);
            // 将prev指向新插入的节点
            prev = prev.next;
            // 将t除以10的商赋值给t
            t /= 10;
        }
        // 返回虚拟头节点的下一个节点，即新链表的头节点
        return newHead.next;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            ListNode head1 = null;
            ListNode tail1 = null;
            int n1 = in.nextInt();
            ListNode l1 = Input(in, head1, tail1, n1);

            ListNode head2 = null;
            ListNode tail2 = null;
            int n2 = in.nextInt();
            ListNode l2 = Input(in, head2, tail2, n2);

            Solution solution = new Solution();
            Print(solution.addTwoNumbers(l1, l2));
        }
    }

    private static void Print(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    private static ListNode Input(Scanner in, ListNode head, ListNode tail, int n) {
        for (int i = 0; i < n; i++) {
            int val = in.nextInt();
            ListNode newNode = new ListNode(val);
            if (head == null) {
                head = newNode;
                tail = newNode;
            } else {
                tail.next = newNode;
                tail = newNode;
            }
        }
        return head;
    }
}